require("Sety")

print("running unit tests on Sety...")

local a = set{1, 2, 3, 4, 5}
local b = set{2, 4, 10}
local c = set{'a', 'b', 'c'}
local d = set{function (a) a = a * 2 return a end, {jello=2, bbq="awesome"}}
local empty = set{}

-- set
assert(a[1] == true, "set 1")
assert(a[10] == false, "set 2")
assert(empty['a'] == false, "set 3")

-- elts
do
    local sum = 0
    for e in set.elts(a) do
        sum = sum + e
    end
    assert(sum == 15, "elts 1")
end
do
    local flag = false
    for e in set.elts(empty) do
        flag = true
    end
    assert(flag == false, "elts 2")
end


-- subset
assert(not set.subset(a,b), "subset 1")
assert(not set.subset(a,empty), "subset 2")
assert(set.subset(set{1}, a), "subset 3")
assert(set.subset(set{}, a), "subset 4")
assert(set.subset(c,c), "subset 5")


-- eq
assert(set.eq(c,c), "eq 1")
assert(set.eq(empty, set{}), "eq 2")
assert(set.eq(a, set{1, 2, 3, 4, 5}), "eq 3")
assert(not set.eq(a, b), "eq 4")
assert(not set.eq(empty, a), "eq 5")
assert(not set.eq(a, empty), "eq 6")


-- proper_subset
assert(not set.proper_subset(a,b), "proper_subset 1")
assert(not set.proper_subset(a,empty), "proper_subset 2")
assert(set.proper_subset(set{1}, a), "proper_subset 3")
assert(set.proper_subset(empty, a), "proper_subset 4")
assert(not set.proper_subset(c,c), "proper_subset 5")
assert(not set.proper_subset(empty, empty), "proper_subset 6")


-- union
assert(set.union(a, b) == set{1, 2, 3, 4, 5, 10}, "union 1")
assert(set.union(a, b) == set.union(b, a), "union 2")
assert(set.union(a, empty) == a, "union 3")
assert(set.union(empty, a) == a, "union 4")

-- intersection
assert(set.intersection(a, b) == set{2, 4}, "intersection 1")
assert(set.intersection(b, a) == set{2, 4}, "intersection 2")
assert(set.intersection(a, empty) == empty, "intersection 3")
assert(set.intersection(empty, a) == empty, "intersection 4")
assert(set.intersection(a, c) == empty, "intersection 5")

-- difference
assert(set.difference(a, b) == set{1, 3, 5}, "difference 1")
assert(set.difference(set{2, 4, 5}, a) == empty, "difference 2")
assert(set.difference(a, empty) == a, "difference 3")
assert(set.difference(empty, a) == empty, "difference 4")

-- add
assert(set.add(a, 12) == set{1, 2, 3, 4, 5, 12}, "add 1")
assert(not (set.add(a, 12) == a), "add 2")
assert(set.add(empty, 'a') == set{'a'}, "add 3")
assert(set.add(a, b) == set{1, 2, 3, 4, 5, 10}, "add 4")

-- sub
assert(set.sub(a, 5) == set{1, 2, 3, 4}, "sub 1")
assert(not (set.sub(a, 5) == a), "sub 2")
assert(set.sub(a, 12) == set{1, 2, 3 ,4 ,5}, "sub 3")
assert(set.sub(a, empty) == a, "sub 4")
assert(set.sub(a, b) == set{1, 3, 5}, "sub 5")

-- copy
assert(set.copy(a) == a, "copy 1")
assert(set.copy(d) == d, "copy 2")

-- cardinality
assert(set.cardinality(a) == 5, "cardinality 1")
assert(set.cardinality(c) == 3, "cardinality 2")
assert(set.cardinality(empty) == 0, "cardinality 3")

-- concat
assert(set.concat(a) == '12345', "concat 1")
assert(set.concat(a, '|') == '1|2|3|4|5', "concat 2")
assert(set.concat(empty) == '', "concat 3")
assert(set.concat(empty, '|') == '', "concat 4")

-- tostring
assert(tostring(set{1}) == '{1}', "tostring 1")
assert(tostring(empty) == '{}', "tostring 2")
assert(tostring(d), "tostring 3")

print("all tests passed")

